package wangyi163

import (
	"encoding/json"
	"errors"
	"fmt"
	"get"
	"git.oschina.net/OO-o/stock/base"
	"git.oschina.net/OO-o/stock/model"
	"runtime"
	//"stocks/fn/Data"
	"sync"
)

//接下来已163的日线数据为例

type Data163 struct {
	Data   [][]interface{}
	Symbol string
	Name   string
}

func (d *Data163) ToData() *model.Data {
	var C = make([]float64, 0, len(d.Data))
	var L = make([]float64, 0, len(d.Data))
	var H = make([]float64, 0, len(d.Data))
	var Date = make([]string, 0, len(d.Data))
	for i := 0; i < len(d.Data); i++ {
		Date = append(Date, d.Data[i][0].(string))
		C = append(C, d.Data[i][2].(float64))
		H = append(H, d.Data[i][3].(float64))
		L = append(L, d.Data[i][4].(float64))

	}
	Datas := &model.Data{
		Code: d.Symbol,
		Name: d.Name,
		C:    &base.Base{},
		H:    &base.Base{},
		L:    &base.Base{},

		Date: Date,
	}
	Datas.C.Set(C)
	Datas.H.Set(H)
	Datas.L.Set(L)
	return Datas

}

func NewData163(code string) (*Data163, error) {
	if code[0] == '0' || code[0] == '3' {
		code = "1" + code
	} else {
		code = "0" + code
	}
	url := "http://img1.money.126.net/data/hs/kline/day/history/2015/" + code + ".json"
	//fmt.Println(url)
	body, err := get.Get(url)
	//fmt.Println(body)
	if err != nil {
		//fmt.Println("get message err!")
		return nil, err
	}
	var data Data163
	err = json.Unmarshal([]byte(body), &data)
	//fmt.Println(data)
	if err != nil {
		//fmt.Println("encoding json err!")
		return nil, err
	}
	if len(data.ToData().Date) == 0 || data.ToData().Date == nil {
		return nil, errors.New("data 数据为空！")
	}
	return &data, nil
}

/*func New163formstring() (*Data163, error) {
	str := `{"data":[["20140102",4.71,4.78,4.79,4.7,1288330,1.06],["20140103",4.75,4.72,4.78,4.69,1156407,-1.26],["20140106",4.72,4.55,4.72,4.51,1824591,-3.6],["20140107",4.56,4.56,4.57,4.48,1106239,0.22],["20140108",4.57,4.51,4.59,4.49,1142702,-1.1],["20140109",4.51,4.45,4.58,4.41,1337285,-1.33],["20140110",4.47,4.44,4.51,4.41,641052,-0.22],["20140113",4.44,4.43,4.49,4.41,584074,-0.23],["20140114",4.43,4.53,4.54,4.39,1120500,2.26],["20140115",4.51,4.52,4.55,4.44,1123162,-0.22],["20140116",4.5,4.53,4.58,4.49,935144,0.22],["20140117",4.5,4.42,4.54,4.39,1044704,-2.43],["20140120",4.4,4.4,4.45,4.38,693019,-0.45],["20140121",4.5,4.84,4.84,4.5,6992762,10.0],["20140122",4.84,4.92,5.02,4.8,8170247,1.65],["20140123",4.85,4.88,5.01,4.83,3507689,-0.81],["20140124",4.82,4.87,4.95,4.82,3765718,-0.2],["20140127",4.89,4.76,4.89,4.74,2773688,-2.26],["20140128",4.75,4.78,4.8,4.7,1716337,0.42],["20140129",4.78,4.77,4.82,4.74,1398232,-0.21],["20140130",4.76,4.75,4.81,4.73,764232,-0.42],["20140207",4.8,4.86,4.87,4.75,1616867,2.32],["20140210",4.85,5.02,5.11,4.85,3727575,3.29],["20140211",5.02,4.94,5.02,4.92,3133350,-1.59],["20140212",4.92,4.97,4.99,4.92,2563316,0.61],["20140213",4.95,4.9,5.02,4.9,2080028,-1.41],["20140214",4.9,4.98,4.99,4.88,2285605,1.63],["20140217",5.01,4.99,5.02,4.95,2331265,0.2],["20140218",4.99,4.92,4.99,4.9,2721259,-1.4],["20140219",4.92,5.11,5.2,4.9,7048102,3.86],["20140220",5.01,4.97,5.09,4.97,4624714,-2.74],["20140221",4.97,4.94,4.99,4.91,2037662,-0.6],["20140224",4.94,4.76,4.94,4.76,2737260,-3.64],["20140225",4.78,4.53,4.8,4.51,2705376,-4.83],["20140226",4.55,4.58,4.59,4.5,1715340,1.1],["20140227",4.6,4.57,4.68,4.57,1957325,-0.22],["20140228",4.55,4.67,4.67,4.54,1052230,2.19],["20140303",4.67,4.69,4.73,4.6,2103998,0.43],["20140304",4.65,4.72,4.72,4.64,1437401,0.64],["20140305",4.72,4.7,4.79,4.67,1011629,-0.42],["20140306",4.75,4.72,4.78,4.6,1815684,0.43],["20140307",4.69,4.67,4.72,4.65,1151119,-1.06],["20140310",4.66,4.6,4.66,4.56,1742227,-1.5],["20140311",4.62,4.69,4.75,4.55,1641404,1.96],["20140312",4.69,4.66,4.71,4.6,1092284,-0.64],["20140313",4.69,4.7,4.7,4.64,1144748,0.86],["20140314",4.7,4.75,4.77,4.66,2941594,1.06],["20140317",4.9,4.92,5.17,4.83,6362717,3.58],["20140318",4.86,4.87,4.92,4.76,3410102,-1.02],["20140319",4.87,4.98,5.02,4.81,4611304,2.26],["20140320",4.94,4.8,4.98,4.76,2659001,-3.61],["20140321",4.79,4.91,4.93,4.74,2333205,2.29],["20140324",4.9,4.92,4.97,4.86,2565476,0.2],["20140325",4.98,4.92,5.01,4.9,2476336,0.0],["20140326",4.93,4.9,4.95,4.84,1916751,-0.41],["20140327",4.94,4.84,4.94,4.84,1657548,-1.22],["20140328",4.82,4.79,4.86,4.77,1651577,-1.03],["20140331",4.8,4.86,4.91,4.78,2286277,1.46],["20140401",4.87,4.81,4.87,4.75,1897453,-1.03],["20140402",4.81,4.94,4.98,4.8,4359126,2.7],["20140403",5.02,4.95,5.17,4.94,6000376,0.2],["20140404",4.9,4.97,4.99,4.9,2932459,0.4],["20140408",4.92,5.0,5.01,4.92,3056128,0.6],["20140409",5.0,5.07,5.09,5.0,4380785,1.4],["20140410",5.1,5.1,5.17,5.03,7435213,0.59],["20140411",5.07,5.01,5.08,4.98,4196407,-1.76],["20140414",5.01,5.15,5.19,4.93,6759730,2.79],["20140415",5.15,5.07,5.15,5.03,3509366,-1.55],["20140416",5.08,5.21,5.22,5.04,7338169,2.76],["20140417",5.25,5.14,5.25,5.14,4099495,-1.34],["20140418",5.13,5.16,5.19,5.12,2383887,0.39],["20140421",5.16,5.0,5.17,4.99,3591240,-3.1],["20140422",4.95,5.05,5.05,4.93,3680557,1.0],["20140423",5.13,5.14,5.14,5.07,3337490,1.78],["20140424",5.15,5.1,5.15,5.05,3037961,-0.78],["20140425",5.09,4.93,5.13,4.92,3105688,-3.33],["20140428",4.88,4.76,4.93,4.74,2433811,-3.45],["20140429",4.75,4.84,4.85,4.75,1659498,1.68],["20140430",4.84,4.88,4.92,4.83,1765366,0.83],["20140505",4.86,4.94,4.95,4.81,1647476,1.23],["20140506",4.93,4.91,4.98,4.88,2048967,-0.61],["20140507",4.9,4.85,4.94,4.84,1718304,-1.22],["20140508",4.89,4.86,4.94,4.82,1890000,0.21],["20140509",4.86,4.87,4.9,4.84,1272141,0.21],["20140512",4.95,4.95,4.99,4.89,2208110,1.64],["20140513",4.93,4.98,5.0,4.93,2270570,0.61],["20140514",4.95,4.94,5.01,4.94,1592255,-0.8],["20140515",4.91,4.84,4.94,4.82,1641057,-2.02],["20140516",4.87,4.85,4.87,4.79,1357752,0.21],["20140519",4.85,4.81,4.87,4.75,1073119,-0.82],["20140520",4.79,4.82,4.87,4.79,756160,0.21],["20140521",4.85,4.87,4.87,4.77,928943,1.04],["20140522",4.87,4.81,4.9,4.79,1588200,-1.23],["20140523",4.85,4.96,5.01,4.79,4561549,3.12],["20140526",4.95,5.03,5.11,4.91,4355125,1.41],["20140527",5.03,5.2,5.25,4.99,12583520,3.38],["20140528",5.12,5.15,5.21,5.09,4182504,-0.96],["20140529",5.16,5.08,5.16,5.07,2448636,-1.36],["20140530",5.08,5.06,5.11,5.01,1720731,-0.39],["20140603",5.05,5.09,5.15,5.05,1650120,0.59],["20140604",5.08,5.06,5.11,5.05,1646513,-0.59],["20140605",5.07,5.09,5.09,5.05,2388211,0.59],["20140606",5.08,5.1,5.17,5.06,2662991,0.2],["20140609",5.11,5.12,5.17,5.06,2583134,0.39],["20140610",5.12,5.17,5.21,5.09,5163902,0.98],["20140611",5.16,5.16,5.19,5.11,1932486,-0.19],["20140612",5.13,5.09,5.18,5.07,2074070,-1.36],["20140613",5.09,5.09,5.13,5.06,1527544,0.0],["20140616",5.11,5.09,5.12,5.08,1677706,0.0],["20140617",5.11,5.11,5.14,5.08,1576902,0.39],["20140618",5.11,5.13,5.14,5.08,1603269,0.39],["20140619",5.13,4.96,5.15,4.92,2194269,-3.31],["20140620",4.93,5.01,5.03,4.93,1065472,1.01],["20140623",5.02,5.04,5.08,5.02,863719,0.6],["20140624",5.05,5.06,5.1,5.02,892994,0.4],["20140625",5.08,5.05,5.1,5.05,1135219,-0.2],["20140626",5.07,5.1,5.12,5.06,1801279,0.99],["20140627",5.12,5.12,5.16,5.07,1876013,0.39],["20140630",5.16,5.12,5.21,5.12,3283943,0.0],["20140701",5.13,5.25,5.31,5.13,6090709,2.54],["20140702",5.27,5.26,5.34,5.2,2806905,0.19],["20140703",5.27,5.26,5.33,5.25,3883122,0.0],["20140704",5.27,5.2,5.28,5.19,2701788,-1.14],["20140707",5.22,5.26,5.28,5.21,1747118,1.15],["20140708",5.27,5.29,5.31,5.2,2625693,0.57],["20140709",5.29,5.25,5.37,5.24,4540171,-0.76],["20140710",5.25,5.41,5.47,5.25,7734517,3.05],["20140711",5.52,5.59,5.81,5.5,15970480,3.33],["20140714",5.52,5.63,5.67,5.42,8850541,0.72],["20140715",5.53,5.55,5.65,5.51,4900721,-1.42],["20140716",5.55,5.4,5.55,5.4,4976407,-2.7],["20140717",5.42,5.67,5.81,5.35,9875722,5.0],["20140718",5.64,5.95,6.15,5.54,12663049,4.94],["20140721",5.95,5.85,6.09,5.8,9005625,-1.68],["20140722",5.88,5.93,6.05,5.8,6726570,1.37],["20140723",5.88,5.95,6.08,5.86,6389061,0.34],["20140724",5.92,5.9,5.97,5.78,5126583,-0.84],["20140725",5.85,5.88,5.93,5.78,4683247,-0.34],["20140728",5.88,5.89,5.95,5.8,8405824,0.17],["20140729",5.89,6.22,6.3,5.85,12629245,5.6],["20140730",6.15,6.19,6.36,6.07,9870760,-0.48],["20140731",6.12,6.19,6.32,6.12,4893091,0.0],["20140801",6.15,6.0,6.2,6.0,6546519,-3.07],["20140804",6.03,6.06,6.1,5.98,7229675,1.0],["20140805",6.08,6.32,6.6,6.03,12523985,4.29],["20140806",6.25,6.28,6.32,6.19,5532432,-0.63],["20140807",6.28,6.14,6.31,6.13,4627359,-2.23],["20140808",6.13,6.2,6.25,6.13,3160258,0.98],["20140811",6.22,6.29,6.29,6.2,4023946,1.45],["20140812",6.27,6.35,6.5,6.22,5851711,0.95],["20140813",6.31,6.27,6.35,6.21,5218986,-1.26],["20140814",6.28,6.2,6.3,6.18,4067256,-1.12],["20140815",6.2,6.2,6.24,6.14,4435104,0.0],["20140818",6.22,6.21,6.25,6.16,4969523,0.16],["20140819",6.23,6.11,6.24,6.04,8802092,-1.61],["20140820",6.11,6.14,6.17,6.08,5462023,0.49],["20140821",6.13,6.06,6.14,6.02,5661509,-1.3],["20140822",6.04,6.11,6.12,6.03,4089200,0.83],["20140825",6.11,6.08,6.12,6.06,3078289,-0.49],["20140826",6.08,6.05,6.15,6.05,3561716,-0.49],["20140827",6.05,5.88,6.08,5.85,4689025,-2.81],["20140828",5.88,5.81,5.93,5.73,2744647,-1.19],["20140829",5.82,5.91,5.92,5.77,2447235,1.72],["20140901",5.95,6.02,6.04,5.93,2970185,1.86],["20140902",6.03,6.04,6.07,5.97,5688805,0.33],["20140903",6.05,6.08,6.12,6.04,4618801,0.66],["20140904",6.1,6.1,6.12,6.05,3612779,0.33],["20140905",6.11,6.11,6.13,6.08,3078820,0.16],["20140909",6.11,6.12,6.16,6.11,4258628,0.16],["20140910",6.11,6.16,6.18,6.07,5596029,0.65],["20140911",6.16,6.17,6.25,6.12,6421184,0.16],["20140912",6.19,6.42,6.45,6.17,12000352,4.05],["20140915",6.45,6.45,6.55,6.34,8230393,0.47],["20140916",6.5,6.18,6.51,6.15,10943792,-4.19],["20140917",6.18,6.28,6.29,6.11,4024530,1.62],["20140918",6.28,6.3,6.33,6.21,3272261,0.32],["20140919",6.28,6.37,6.38,6.28,4788629,1.11],["20140922",6.38,6.26,6.42,6.15,8046532,-1.73],["20140923",6.17,6.26,6.26,6.15,5344409,1.79],["20140924",6.24,6.3,6.3,6.22,5607654,0.64],["20140925",6.29,6.24,6.35,6.23,4480129,-0.95],["20140926",6.21,6.31,6.31,6.18,4710514,1.12],["20140929",6.33,6.36,6.36,6.3,4521538,0.79],["20140930",6.38,6.51,6.54,6.35,7188877,2.36],["20141008",6.61,6.69,6.86,6.61,11495786,2.76],["20141009",6.67,6.73,6.76,6.57,8536096,0.6],["20141010",6.73,6.77,6.91,6.6,7212061,0.59],["20141013",6.73,7.04,7.17,6.7,8949329,3.99],["20141014",7.11,6.95,7.19,6.92,8565930,-1.28],["20141015",7.04,7.07,7.18,6.96,7248516,1.73],["20141016",7.03,6.94,7.23,6.94,6391811,-1.84],["20141017",6.9,6.78,6.99,6.61,6388133,-2.31],["20141020",6.81,7.01,7.09,6.81,4828668,3.39],["20141021",7.08,6.92,7.08,6.89,3755942,-1.28],["20141022",6.92,6.71,6.98,6.7,4192083,-3.03],["20141023",6.69,6.59,6.76,6.57,3066113,-1.79],["20141024",6.59,6.7,6.75,6.59,2980582,1.67],["20141027",6.69,6.82,7.03,6.59,5265847,1.79],["20141028",6.95,7.18,7.25,6.87,9550051,5.28],["20141029",7.48,7.17,7.49,7.12,11239598,-0.14],["20141030",7.18,7.2,7.33,7.08,7680450,0.42],["20141031",7.22,7.18,7.25,7.1,5012790,-0.28],["20141103",7.2,7.32,7.35,7.2,7285157,1.95],["20141104",7.31,7.18,7.31,7.16,5035868,-1.91],["20141105",7.2,7.17,7.29,7.16,3685925,-0.14],["20141106",7.16,7.23,7.25,7.11,3832043,0.84],["20141107",7.23,7.17,7.46,7.16,7987733,-0.83],["20141110",7.2,7.53,7.58,7.2,12860524,5.02],["20141111",7.52,7.35,7.6,7.26,9451612,-2.39],["20141112",7.38,7.57,7.57,7.32,5626041,2.99],["20141113",7.6,7.67,7.74,7.46,7737636,1.32],["20141114",7.59,7.61,7.7,7.46,5363557,-0.78],["20141117",7.66,7.57,7.7,7.52,3940634,-0.53],["20141118",7.82,7.76,8.17,7.72,12085283,2.51],["20141119",7.76,7.76,7.85,7.57,5780331,0.0],["20141120",7.66,7.66,7.79,7.65,3412190,-1.29],["20141121",7.55,7.69,7.74,7.55,3417552,0.39],["20141124",7.77,7.86,7.97,7.71,6636318,2.21],["20141125",7.83,8.13,8.19,7.78,8888213,3.44],["20141126",8.15,8.07,8.19,7.96,6348525,-0.74],["20141127",8.05,8.05,8.13,7.97,4759133,-0.25],["20141128",8.09,8.29,8.3,8.04,8612783,2.98],["20141201",8.38,8.43,8.54,8.08,12575970,1.69],["20141202",8.43,8.48,8.5,8.3,5534667,0.59],["20141203",8.5,8.49,8.68,8.38,6840701,0.12],["20141204",8.48,8.62,8.66,8.43,6228869,1.53],["20141205",8.65,8.31,8.68,8.0,7554119,-3.6],["20141208",8.28,8.21,8.39,8.0,6483136,-1.2],["20141209",8.28,8.06,8.64,8.01,12010789,-1.83],["20141210",8.03,8.3,8.34,8.02,5776138,2.98],["20141211",8.31,8.51,8.52,8.3,6722179,2.53],["20141212",8.53,8.9,8.95,8.44,12630596,4.58],["20141215",8.83,9.19,9.25,8.58,9485420,3.26],["20141216",9.14,9.18,9.32,9.06,6658241,-0.11],["20141217",9.18,9.24,9.31,9.0,5542058,0.65],["20141218",9.26,9.19,9.32,9.05,6123310,-0.54],["20141219",9.2,9.26,9.32,9.05,8046443,0.76],["20141222",9.28,8.89,9.3,8.33,10284057,-4.0],["20141223",8.79,8.9,9.04,8.6,3974983,0.11],["20141224",8.89,9.12,9.19,8.82,5996178,2.47],["20141225",9.2,9.7,9.8,9.1,19141574,6.36],["20141226",9.65,9.71,9.89,9.41,10586241,0.1],["20141229",9.79,9.64,9.95,9.47,12928866,-0.72],["20141230",9.64,9.4,9.84,9.3,23413704,-2.49],["20141231",9.25,9.04,9.68,8.48,20656163,-3.83]],"symbol":"000672","name":"\u4e0a\u5cf0\u6c34\u6ce5"}`
	var data Data163
	err := json.Unmarshal([]byte(str), &data)
	//fmt.Println(data)
	if err != nil {
		fmt.Println("encoding json err!")
		return nil, err
	}
	return &data, nil
}*/
func init() {
	runtime.GOMAXPROCS(runtime.NumCPU())
}
func Run(codes []string) {
	var wait sync.WaitGroup
	c := make(chan string)
	wait.Add(runtime.NumCPU())
	for i := 0; i < runtime.NumCPU(); i++ {
		go func() {
			for item := range c {
				//	fmt.Println("已经执行", item)
				info := Do(item)
				if len(info) > 0 {
					fmt.Println("info:", info)
				}
			}
			wait.Done()
		}()
	}

	for _, item := range codes {
		c <- item

	}
	close(c)

	wait.Wait()
}
func Do(code string) []model.Info {
	data, err := NewData163(code)
	//fmt.Println(data)
	if err != nil {
		//fmt.Println(err)
		return nil
	}
	return model.Model001(data, 1)
	/*if len(info) > 0 {
		fmt.Println("info:", info)
	}
	*/
}
